<?php

/**
 * Mid-advanced file lister.
 *
 * @package				fLister
 * @version				3.0-ALPHA1
 * @author				Sebastian Dudek <contact@dedikownia.org>
 * @link					http://code.google.com/p/flister/
 * @license				http://opensource.org/licenses/gpl-license.php GNU General Public License v3
 */

/*
 * 
 */
class SortableDirectoryIterator implements IteratorAggregate {
  private $_storage;

  /*
   * 
   */
  public function __construct($path)
  {
    $this->_storage = new ArrayObject();

    $files = new ExtendsDirectoryIterator($path);
        
    foreach ($files as $file) {
      $this->_storage->offsetSet($file->getFilename(), clone $file);
    }
    
    $this->_storage->ksort();
  }
  
  /*
   * 
   */
  public function getIterator()
  {
    return $this->_storage->getIterator();
  }
}

/*
 * 
 */
class ExtendsDirectoryIterator extends DirectoryIterator {
  
  /*
   * 
   */
  public function getBSize()
  {
    $size = $this->getSize();
       
    if($this->isDir() && fLister::$config['dirSize']) {
      $size = 0; 
      
      foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getPathname())) as $file) { 
        $size += $file->getSize(); 
      }
      
    } elseif($this->isDir()) {
      return '---';
    }
    
    $s = array('B', 'KB', 'MB', 'GB', 'TB');
    $e = floor(log($size) / log(1024));
    
    if(pow(1024, floor($e)) != 0) {
      return sprintf('%.2f'.$s[$e], ($size / pow(1024, floor($e))));
    } else {
      return '0B';
    }
  }
  
  /*
   * Metoda formatująca datę ostatniej modyfikacji pliku/folderu.
   * 
   * @access public
   * @return string sformatowana data
   */
  public function getFormatMTime($date)
  {
    return date(fLister::config['dateFormat'], $this->getMTime());
  }
}